package com.ericson.pgm.log.analyser.formater;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ThreadFormat implements FormatInterceptor {

	private static String linePattern = "^\\[#(\\|(.+)\\|)#\\]$";
	private static String contentPattern = "(;.+;)";

	private String xmlVersion = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\t<?xml-stylesheet type=\"text/xsl\" href=\"pgm_log_analyzer.xsl\"?>";

	@Override
	public OutputStream format(InputStream file) {
		// TODO Auto-generated method stub

		ByteArrayOutputStream bao = new ByteArrayOutputStream();

		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(file));
			Writer out = new OutputStreamWriter(bao, "utf-8");
			BufferedWriter bw = new BufferedWriter(out);
			String line = null;
			bw.write(xmlVersion + "\n");
			StringBuilder sb = null;
			String lastThreadId = null;
			String lastTime = null;
			while ((line = br.readLine()) != null) {
				sb = new StringBuilder();
				if (line.isEmpty()) {
					continue;
				}
				String[] ret = extractInfos(line);
				if ((lastThreadId == null) || !lastThreadId.equals(ret[1])) {
					lastThreadId = ret[1];
					sb.append("<Thread id=\"").append(ret[1])
							.append("\" name=\"").append(ret[2]).append("\">");
				}
				if ((lastTime == null) || !lastTime.equals(ret[0])) {
					lastTime = ret[0];
					sb.append("\n\t<time timestamp=\"").append(ret[0])
							.append("\" requestId=\"").append(ret[4])
							.append("\">");
				}
				sb.append("\n\t\t<class>");
				sb.append("\n\t\t\t<name>").append("\n\t\t\t\t").append(ret[3])
						.append("\n\t\t\t</name>");
				sb.append("\n\t\t\t<log>").append("\n\t\t\t\t").append(ret[5])
						.append("\n\t\t\t</log>");
				sb.append("\n\t\t</class>");

				if ((lastTime == null) || !lastTime.equals(ret[0])) {
					sb.append("\n\t</time>");
				}

				if ((lastThreadId == null) || !lastThreadId.equals(ret[1])) {
					sb.append("\n</Thread>");
				}

				bw.write(sb.toString());
			}
			bw.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return bao;
	}

	/**
	 * 
	 * @param line
	 * @return 0-time <br/>
	 *         1-threadId <br/>
	 *         2-ThreadName<br/>
	 *         3-className<br/>
	 *         4-requestId <br/>
	 *         5-log content
	 */

	private String[] extractInfos(String line) {
		Pattern p = Pattern.compile(linePattern);
		Matcher m = p.matcher(line);
		String[] ret = new String[6];
		if (m.matches()) {
			String temp = m.group(2);
			String[] splits = temp.split("\\|");
			ret[0] = splits[0];
			ret[5] = splits[5];
			splits = splits[4].split(";");
			ret[1] = splits[0].split("=")[1];
			ret[2] = splits[1].split("=")[1];
			ret[3] = splits[2].split("=")[1];
			ret[4] = splits[4].split("=")[1];

		}

		return null;

	}

	@Override
	public OutputStream[] format(InputStream[] files) {
		// TODO Auto-generated method stub
		return null;
	}

}
